package com.jgp.timing.controller;

import com.jgp.common.annotation.UIParam;
import com.jgp.sys.controller.JGPBackController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import com.jgp.timing.model.TimingJob;
import com.jgp.timing.service.JobActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019/3/27
 */
@RestController
@RequestMapping("timing/job-api-ctl")
public class JobApiController extends JGPBackController {
    
    @Autowired
    private JobActionService jobActionService;
    
    @PostMapping("list-jobs")
    public Result listJobs(@UIParam("pager") Pager pager){
        List<TimingJob> jobs = jobActionService.queryAll(pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("任务名","name"));
        categories.add(new GridResult.Category("状态","started"));
        categories.add(new GridResult.Category("执行次数","times"));
        categories.add(new GridResult.Category("定时表达式","cron"));
        return ajaxReGrid("gdata",categories,jobs);
    }
    
    @PostMapping("start-job")
    public Result startJob(Long jobId){
        jobActionService.start(jobId);
        return ajaxRe(true);
    }
    @PostMapping("stop-job")
    public Result stopJob(Long jobId){
        jobActionService.stop(jobId);
        return ajaxRe(true);
    }
    @PostMapping("remove-job")
    public Result removeJob(Long jobId){
        return ajaxRe(jobActionService.remove(jobId));
    }
    
    @PostMapping("restart-job")
    public Result restartJob(Long jobId){
        jobActionService.restart(jobId);
        return ajaxRe(true);
    }
    
    @PostMapping("query-job")
    public Result queryJob(Long jobId){
        return ajaxReForm("fdata",jobActionService.queryJob(jobId));
    }
    
    
    @PostMapping("create-job")
    public Result saveJob(TimingJob job){
        return ajaxReForm("fdata",jobActionService.createJob(job));
    }
    
    @PostMapping("update-job")
    public Result updateJob(TimingJob job){
        return ajaxReForm("fdata",jobActionService.updateJob(job));
    }
    
    
    
    /*@PutMapping("task")
    public Result task(@RequestBody List<TimingJob> tasks, BindingResult result){
        return ajaxReData("task",tasks);
    }
    
    
    @PutMapping("task2")
    public Result task2(@Validated @UIParam TimingJob task){
        return ajaxReData("task",task);
    }
    
    @PutMapping("task3")
    public Result task3(@Validated @UIParam List<TimingJob> tasks){
        return ajaxReData("task",tasks);
    }*/
    
    
}

